"use strict";
/* Controllers */
app.controller("userAddController", ['$scope', '$location', '$rootScope', '$routeParams', 'userAddService', function ($scope, $location, $rootScope, $routeParams, userAddService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.id = $routeParams.id; // 如果ID!=0为更新
    vm.d = {};
    vm.d.isTestDefine = [{id:0, name:'0-正式'},{id:1, name:'1-测试'}];
    var selectIsTestDefine = function(id) {
        for(var i=0;i<vm.d.isTestDefine.length; ++i) {
            var item = vm.d.isTestDefine[i];
            if(item.id==id) {
                return item;
            }
        }
        return vm.d.isTestDefine[0];
    };

    var errorFn = function (result) {
        alert(result);
    };
    $scope.addUser = function () {
        var vm = $scope.vm;
        var users = {
            id: vm.p.id,
            userName: vm.p.userName,
            userCn: vm.p.userCn,
            email: vm.p.email,
            mobile: vm.p.mobile,
            isTest: vm.p.isTestDefine.id,
            leader: vm.p.leader
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                //window.location = "#/income/user_list/0";
                alert("执行成功！")
            } else {
                alert(response.msg);
            }
        };
        userAddService.addUser(users, procFn, errorFn);
    };

    if(vm.p.id!=0) {
        var users = {
            id: vm.p.id
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                vm.p = response.data;
                vm.p.isTestDefine = selectIsTestDefine(vm.p.isTest);
            } else {
                alert(response.message);
            }
        };
        userAddService.getUser(users, procFn, errorFn);
    }else {
        vm.p.isTestDefine = selectIsTestDefine(0);
    }

}]);


/* Services */
app.service('userAddService', ['ajaxService', function (ajaxService) {

    this.addUser = function (users, successFunction, errorFunction) {
        ajaxService.AjaxPost(users, "/star/api/basic-service/v1/user/addUser.do", successFunction, errorFunction);
    };

    this.getUser = function (users, successFunction, errorFunction) {
        ajaxService.AjaxPost(users, "/star/api/basic-service/v1/user/getUser.do", successFunction, errorFunction);
    };

}]);

